package com.xiaoma.thread.wait.productcustomer;

/**
 * @author maj
 * @ClassName SynStack
 * @Description
 * @date 2020-09-12 13:53
 */
public class SynStack {

  private char[] data = new char[6];

  private int cnt = 0; //表示数组有效元素的个数

  public synchronized void push(char ch) {
    while (cnt >= data.length) {
      try {
        System.out.println("生产线程" + Thread.currentThread().getName() + "准备休眠");
        this.wait();
        System.out.println("生产线程" + Thread.currentThread().getName() + "休眠结束了");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    this.notify();
    data[cnt] = ch;
    ++cnt;
    System.out.printf("生产线程" + Thread.currentThread().getName() + "正在生产第%d个产品，该产品是: %c\n", cnt, ch);
  }

  public synchronized char pop() {
    char ch;
    while (cnt <= 0) {
      try {
        System.out.println("消费线程" + Thread.currentThread().getName() + "准备休眠");
        this.wait();
        System.out.println("消费线程" + Thread.currentThread().getName() + "休眠结束了");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    this.notify();
    ch = data[cnt - 1];
    System.out.printf("消费线程" + Thread.currentThread().getName() + "正在消费第%d个产品，该产品是: %c\n", cnt, ch);
    --cnt;
    return ch;
  }
}
